﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WinFromMediaPlayerDemo.Common;
using WinFromMediaPlayerDemo.Interface;
namespace WinFromMediaPlayerDemo.Forms
{
    public partial class SettingForm : ToolFormBase
    {
        /// <summary>
        /// 检查配置文件是否配置
        /// </summary>
        public static bool CheckSettings()
        {
            if(string.IsNullOrEmpty(Global.Default.LocalCacheDirecotry) ||
                string.IsNullOrEmpty(Global.Default.VideoSererAddress) ||
                string.IsNullOrEmpty(Global.Default.SignalRServerAddress) ||
                string.IsNullOrEmpty(Global.Default.VideoDownloadAddress)
            )
            {
                return false;
            }
            return true;
        }

        private IWMPMangerProvider _WMPMangerProvider;
        public SettingForm(IWMPMangerProvider wMPManger)
        {
            InitializeComponent();
            this.txtCacheDirectory.Text = Global.Default.LocalCacheDirecotry;
            this.txtVideoSourceAddr.Text = Global.Default.VideoSererAddress;
            this.txtSignalRServerAddr.Text = Global.Default.SignalRServerAddress;
            this.txtDownloadUrl.Text = Global.Default.VideoDownloadAddress;
            this.numPartionSize.Value = Global.Default.PartionSize;
            this._WMPMangerProvider = wMPManger;
            _WMPMangerProvider.ActionManager(Common.WMPActionType.Stop);
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtVideoSourceAddr.Text))
            {
                MessageBox.Show(this, "请输入视频资源地址", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if(string.IsNullOrEmpty(txtDownloadUrl.Text))
            {
                MessageBox.Show(this, "请输入视频下载地址", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if(string.IsNullOrEmpty(txtSignalRServerAddr.Text))
            {
                MessageBox.Show(this, "请输入SignalR服务器地址", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if(string.IsNullOrEmpty(txtCacheDirectory.Text))
            {
                MessageBox.Show(this, "请选择缓存目录", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else if(!System.IO.Directory.Exists(txtCacheDirectory.Text))
            {
                MessageBox.Show(this, "缓存目录不存在", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Global.Default.VideoSererAddress = this.txtVideoSourceAddr.Text;
                Global.Default.SignalRServerAddress = this.txtSignalRServerAddr.Text;
                Global.Default.LocalCacheDirecotry = this.txtCacheDirectory.Text;
                Global.Default.VideoDownloadAddress = this.txtDownloadUrl.Text;
                Global.Default.Save();
                Global.Default.Reload();
                Global.Default.PartionSize = (ulong)this.numPartionSize.Value;
                //重启应用程序
                Application.Restart();
                //this.Close();
                
            }
        }

        private void banCancle_Click(object sender, EventArgs e)
        {
            _WMPMangerProvider.ActionManager(Common.WMPActionType.Play);
            this.Close();
        }

        private void btnSelCachePath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog browserDialog = new FolderBrowserDialog();
            browserDialog.SelectedPath = this.txtCacheDirectory.Text;
            if(browserDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.txtCacheDirectory.Text = browserDialog.SelectedPath;
            }
            
        }
    }
}
